package com.gemme.gemme.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.R;
import com.gemme.gemme.gwc.ConfirmOrderActivity;
import com.gemme.gemme.gwc.OnProductBean;
import com.gemme.gemme.gwc.PaySucessActivity;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.util.ActivityCollectorUtil;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private final String TAG = "WXPayEntryActivity";

    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        setContentView(R.layout.activity_wxpay_entry);
        api = WXAPIFactory.createWXAPI(this, Constant.weixin_appid);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
//        setContentView(R.layout.activity_wxpay_entry);
        api = WXAPIFactory.createWXAPI(this, Constant.weixin_appid);
        api.handleIntent(getIntent(), this);
    }







    @Override
    public void onReq(BaseReq baseReq) {
        Log.i(TAG, "onReq: " + baseReq.toString());
    }


    /**
     * @auther WangQX
     * created at 2019/3/11 0011
     * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2
     * errCode = -1  //签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
     */
    @Override
    public void onResp(BaseResp baseResp) {
        Log.i(TAG, "onResp: ");
        Log.i(TAG, "onPayFinish,errCode=" + baseResp.errCode);
        if (baseResp.errCode == BaseResp.ErrCode.ERR_COMM) {
//            showToast(getResources().getString(R.string.recharge_failed));
            System.out.println("R.string.recharge_failed)");
        } else if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
//            showToast(getResources().getString(R.string.recharge_successful));
            System.out.println("R.string.recharge_successful)");
        }
//        closeConfirmOrderActivity();
        if(baseResp.errCode != 0){//支付不成功不进入支付成功页面
            return;
        }


        try {
            PayResp resp = (PayResp) baseResp;
//            String payType = resp.extData;
            OnProductBean bean = JSON.parseObject(resp.extData, OnProductBean.class);
            String payType = bean.getType();
            switch (payType) {
                case Constant.type_order_pay:        //订单支付的回调
                    // TODO: doSomething

                    Intent intent = new Intent(this, PaySucessActivity.class);

                    intent.putExtra("data",JSON.toJSONString(bean));
                    startActivity(intent);
                    break;
                case "RechargePay":     //充值支付的回调
                    // TODO: doSomething
                    break;
            }
        }catch (Exception e){

        }

        finish();
    }

    private void closeConfirmOrderActivity() {
        ArrayList<Activity> activities = ActivityCollectorUtil.getmActivityList();
        for (int i = 0; i < activities.size(); i++) {
            Activity act = activities.get(i);
            if(act instanceof ConfirmOrderActivity){
                act.finish();
                ActivityCollectorUtil.removeActivity(act);
                return;
            }
        }
    }

}
